package se.jagvetintedu;

import java.util.ArrayList;

public class prime {
	
	int generateCount(int count) {
		ArrayList<Integer> primes = new ArrayList<Integer>();
		primes.add(2);
		
		int current = 3;
		int increment = 2;
		
		boolean prime = false;
		
		while (primes.size() < count) {
			for (Integer i : primes) {
				prime = true;
				if (current % i == 0) {
					current += increment;
					prime = false;
					break;
				}
			}
			if (prime) {
				primes.add(current);
				current += increment;
			}
			
		}

		return primes.get(primes.size()-1);
	}
	
	ArrayList<Integer> generate(int max) {

		Integer[] temp = new Integer[max];
		ArrayList<Integer> result = new ArrayList<Integer>();
		
		for (int t = 2; t<max; t++) {
			temp[t] = t;
		}

		for (Integer i : temp) {
			if (i != null) {
				result.add(i);
				for (int t = 2*i; t<max; t+=i) {
					temp[t] = null;
				}
			} 
		}

		return result;
	}
}